package com.example.student.controller;

import com.example.student.generated.entity.Class;
import com.example.student.generated.entity.Student;
import com.example.student.generated.service.IClassService;
import com.example.student.service.MyClassService;
import com.example.student.service.MyStudentService;
import com.exmaple.result.ErrorCode;
import com.exmaple.result.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**log：
 * 2022-11-25
 * 并不完善，只添加了两个关于学生表和班级表的操作
 */


@RestController
@RequestMapping(value = "/class")
public class ClassController {
    @Resource
    private IClassService classService;

    @Resource
    private MyStudentService myStudentService;

    @Resource
    private MyClassService myClassService;

    /**
     * 查询所有班级和班级中的学生信息
     * @return
     */
    @GetMapping(value = "/all/class")
    public Result<List<Class>> getAllClass()
    {
        List<Class> classes= classService.list();
        if (classes.isEmpty())
            return Result.error(ErrorCode.USER_ERROR_A0201);
        List<Class> result =new ArrayList<>();
        for (Class oneClass : classes) {
            List<Student> students = myStudentService.lambdaQuery().eq(Student::getClassId, oneClass.getId()).list();
            oneClass.setStudentList(students);
            result.add(oneClass);
        }
        return Result.success(result);
    }

    /**
     * 根据班级号查询班级里的所有学生
     * @param id
     * @return
     */
    @GetMapping(value = "/all/student")
    public Result<List<Student>> getAllStudent(@RequestParam Long id)
    {
        List<Student> studentList = myStudentService.lambdaQuery().eq(Student::getClassId, id).list();
        return studentList.isEmpty()?Result.error(ErrorCode.USER_ERROR_A0201):Result.success(studentList);
    }

    /**
     * 查询所有班级和班级中的学生信息
     * @return
     */
    @GetMapping(value = "/xml/class")
    public Result<List<Class>> getAllClassXml()
    {
        List<Class> classList = myClassService.getAllClassXml();
        return classList.isEmpty()?Result.error(ErrorCode.USER_ERROR_A0201):Result.success(classList);
    }

    /**
     * 根据班级号查询班级里的所有学生
     * @param id
     * @return
     */
    @GetMapping(value = "/xml/student")
    public Result<List<Student>> getAllStudentXml(@RequestParam Long id)
    {
        List<Student> studentList=myClassService.getStudentById(id);
        return studentList.isEmpty()?Result.error(ErrorCode.USER_ERROR_A0201):Result.success(studentList);
    }
}
